package com.qvtu.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.qvtu.springboot.Common.Result;
import com.qvtu.springboot.entity.Article;
import com.qvtu.springboot.service.ArticleService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

//    新增数据
    @PostMapping("/add")
    public Result add(@RequestBody Article aritcle){
        articleService.add(aritcle);
        return Result.success();
    }

//    删除数据
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        articleService.delete(id);
        return Result.success();
    }

    //    批量删除数据
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        articleService.deleteBatch(ids);
        return Result.success();
    }

//   修改数据
   @PutMapping("/update")
    public Result update(@RequestBody Article article){
        articleService.update(article);
        return Result.success();
    }

//    查询数据
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Article article=articleService.selectById(id);
        return Result.success(article);
    }

//    查询所有数据
//    get请求没有请求体 所以不需要RequestBody
    @GetMapping("selectAll")
    public Result selectAll(Article article){
        List<Article> list=articleService.selectAll(article);
        return Result.success(list);
    }

    @GetMapping("selectPage")
    public Result selectPage(Article article, @RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Article> pageInfo=articleService.selectPage(article,pageNum,pageSize);
        return Result.success(pageInfo);
    }
}
